package cn.kogee.system.service.impl;

import cn.kogee.system.domain.Role;
import cn.kogee.system.mapper.RoleMapper;
import cn.kogee.system.service.IRoleService;
import cn.kogee.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    /**
     * 重写新增
     * @param role
     */
    @Override
    @Transactional
    public void add(Role role) {
        super.add(role);
        addRoleMenuAndPermission(role);
    }

    private void addRoleMenuAndPermission(Role role) {
        List<Long> menus = role.getMenus();
        if(menus.size()>0){//用户勾选了菜单
            roleMapper.addRoleMenu(role);
        }
        List<Long> permissions = role.getPermissions();
        if(permissions.size()>0){
            roleMapper.addRolePermission(role);

        }
    }

    /**
     * 重写删除
     * @param id
     */
    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        //删除角色关联表信息
        roleMapper.delByMenu(id);
        //删除菜单关联表信息
        roleMapper.delByPermission(id);
        //删除员工关联表信息
        roleMapper.delByEmployee(id);
    }

    /**
     * 重写修改
     * @param role
     */
    @Override
    @Transactional
    public void update(Role role) {
        super.update(role);
        //删除之前的新建的表信息
        roleMapper.delByMenu(role.getId());
        roleMapper.delByPermission(role.getId());
    }
}
